package com.ruoyi.project.ins.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.ins.domain.InsRegister;
import com.ruoyi.project.ins.enums.RegisterStatusEnum;
import com.ruoyi.project.ins.service.IInsRegisterService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/ins/register")
public class InsRegisterController extends BaseController {
    @Autowired
    private IInsRegisterService insRegisterService;

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<InsRegister> util = new ExcelUtil<>(InsRegister.class);
        util.importTemplateExcel(response, "账号注册");
    }

    @PreAuthorize("@ss.hasPermi('ins:register:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsRegister insRegister)
    {
        String username = getUsername();
        if (!username.equalsIgnoreCase("admin")) {
            insRegister.setCreateBy(username);
        }
        startPage();
        List<InsRegister> list = insRegisterService.selectRegisterList(insRegister);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(insRegisterService.selectRegisterById(id));
    }

    @Log(title = "ins注册", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('ins:register:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, InsRegister insRegister)
    {
        String username = getUsername();
        if (!username.equalsIgnoreCase("admin")) {
            insRegister.setCreateBy(username);
        }
        List<InsRegister> list = insRegisterService.selectRegisterList(insRegister);
        ExcelUtil<InsRegister> util = new ExcelUtil<>(InsRegister.class);
        util.exportExcel(response, list, "ins注册数据");
    }

    @Log(title = "ins注册", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('ins:register:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<InsRegister> util = new ExcelUtil<>(InsRegister.class);
        List<InsRegister> registerList = util.importExcel(file.getInputStream());
        String operateName = getUsername();
        String message = insRegisterService.importRegister(registerList, operateName);
        return success(message);
    }

    @PreAuthorize("@ss.hasPermi('ins:register:add')")
    @Log(title = "ins注册", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody InsRegister insRegister)
    {
        InsRegister ins = insRegisterService.selectByEmail(insRegister.getEmailAccount());
        if (ObjectUtils.isNotEmpty(ins)) {
            return error("该邮箱已被使用：" + insRegister.getEmailAccount());
        }
        insRegister.setCreateBy(getUsername());
        insRegister.setDefaultStatus();
        insRegister.setRandomUserAccount();
        insRegister.generateCodeAddress();
        return toAjax(insRegisterService.insertInsRegister(insRegister));
    }

    @PreAuthorize("@ss.hasPermi('ins:register:edit')")
    @Log(title = "ins注册", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody InsRegister insRegister)
    {
        if (!RegisterStatusEnum.UNREGISTERED.getCode().equals(insRegister.getStatus())) {
            return error("仅未注册的账号可修改");
        }
        insRegister.setUpdateBy(getUsername());
        return toAjax(insRegisterService.updateInsRegister(insRegister));
    }

    @PreAuthorize("@ss.hasPermi('ins:register:remove')")
    @Log(title = "ins注册", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deleteIds}")
    public AjaxResult remove(@PathVariable Long[] deleteIds)
    {
        List<InsRegister> list = insRegisterService.selectByIds(deleteIds);
        boolean canNotDelete = list.stream().anyMatch(
                insRegister -> !RegisterStatusEnum.UNREGISTERED.getCode().equals(insRegister.getStatus()));
        if (canNotDelete) {
            return error("仅未注册的账号可删除, 请重新选择");
        }
        return toAjax(insRegisterService.deleteByIds(deleteIds));
    }
}
